package com.hp.bon.sgw.wh.channel.especial.bizSlot;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.wh.channel.common.BizConsts;
import com.hp.bon.sgw.wh.channel.common.BizParam;
import com.hp.bon.sgw.wh.channel.common.BizResult;
import com.hp.bon.sgw.wh.channel.outside.BizSlot;
import com.hp.bon.sgw.wh.channel.outside.bizSlotOutQuery.utils.HttpClientUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class RechargedBizSlot implements BizSlot {
    private static final Logger _log = LoggerFactory.getLogger(RechargedBizSlot.class);

    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();
        try {
            String paymentSerialNbr = bizParam.getParamString(BizConsts.KEY_ESPECIAL_PAYMENT_SERIAL_NBR);
            String bankCodeSet = bizParam.getParamString(BizConsts.KEY_ESPECIAL_BANK_CODE_SET);
            String accountVal = bizParam.getParamString(BizConsts.KEY_ESPECIAL_ACCOUNTVAL);
            String bankCode = bizParam.getParamString(BizConsts.KEY_ESPECIAL_BANK_CODE);
            String spuId = bizParam.getParamString(BizConsts.KEY_ESPECIAL_SPUID);


            String chargeSource = "";
            String[] bankCodeSets = bankCodeSet.split(",");
            for (String item : bankCodeSets) {
                if (String.valueOf(item.split("\\|")[0]).equals(bankCode)) {
                    chargeSource = String.valueOf(item.split("\\|")[1]);
                }
            }


            JSONObject recharge = new JSONObject();
            recharge.put("reqSerial", paymentSerialNbr);
            recharge.put("chargeSource", chargeSource);
            recharge.put("destinationId", accountVal);
            recharge.put("destinationIdType", "2");
            recharge.put("destinationAttr", "2");
            recharge.put("balanceItemTypeID", "0");
            recharge.put("unitTypeID", "0");
            //"feeAmount":0,  --根据入参spuId取到的映射值，参考前面充值流程Payment_Amount ???
            recharge.put("feeAmount", spuId);
            recharge.put("BankCode", bankCode);
            recharge.put("PaymentMethod", "11");
            JSONObject stdCcrRecharge = new JSONObject();
            stdCcrRecharge.put("recharge", recharge);

            com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
            jsonObject.put("stdCcrRecharge", stdCcrRecharge);

            StringBuilder buildStringMes = new StringBuilder();
            buildStringMes.append(jsonObject.toJSONString());
            //新接口新域名
            String webserviceUrl = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);

            String resp = HttpClientUtils.postHttpStr(buildStringMes.toString(), webserviceUrl);
            if (resp.equals("")) {
                resp = com.hp.bon.sgw.wh.channel.invoices.bizSlot.HttpClientUtils.getErrorJsonStr(BizConsts.STATUS_ACOUNT_API_FAIL, "", "", "");
            }
            bizResult.setHttpBody(resp);
        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            _log.info(e.toString());
        }
        return bizResult;
    }
}
